/*
   OpenZone2, Everquest 2 data editor
   Copyright (C) 2008  Andrew Fenn
    
	OpenZone2 is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   OpenZone2 is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with OpenZone2.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "OZContentMgr.h"

OzContentMgr::OzContentMgr() { }

OzContentMgr::~OzContentMgr() { }

bool OzContentMgr::loadEQ2File(std::string filename, OZ2File& data) {
	// open and check the file
	mFile.open(filename.c_str(),std::ios::binary|std::ios::in);
	if(mFile.fail() ) {
	   std::cout << "Error reading file: " << filename << std::endl;
	   mFile.clear();
	   return false;
	}
	
	mFileType = getEQ2FileType();
	if (mFileType.compare("VeRenderMesh") == 0) {
		getEQ2DrawData(data);
	}
	mFile.close();
	
	return true;
}

std::string OzContentMgr::getEQ2FileType() {
	std::string format;
	char tempchar[1] = {0};
	int stop = 0;
	char name[16] = {0};
	mFile.read(tempchar,1); // ignore first byte
	for (int i=0; i < 16; i++) {
			mFile.read(tempchar,1);
			memcpy(&stop, tempchar, 1);
			if (stop == 2) break;
			name[i] = tempchar[0];
	}
	format = std::string(name);
	return format;
}

void OzContentMgr::getVertices(long length, OZ2File& data) {
	data.vertLength = length;
	data.vertices = new Vector3[length];
 
	for (long i=0; i < length; i++) {
			mFile.read(buffer, 4);
			memcpy(&data.vertices[i].x, buffer, 4);
 
			mFile.read(buffer, 4);
			memcpy(&data.vertices[i].y, buffer, 4);
 
			mFile.read(buffer, 4);
			memcpy(&data.vertices[i].z, buffer, 4);
	}
}

void OzContentMgr::getEQ2DrawData(OZ2File& data) {
 
	unsigned int length, i;
	unsigned short flags, version;
 
	mFile.read(buffer, 2); // get the version number
	memcpy(&version, buffer, 2);
	std::cout << "Version: " << version << std::endl;

	// get file flags
	mFile.read(buffer, 4);
	memcpy(&flags, buffer, 4);
	std::cout << "File flags: " << flags << std::endl;
		
	
	// Get Vertices
 	mFile.read(buffer, 4);
	memcpy(&length, buffer, 4);
	std::cout << "Vertices Length: " << length << std::endl;
 	getVertices(length, data);

   // Get Normals
	mFile.read(buffer, 4);
	memcpy(&length, buffer, 4);
	std::cout << "Normals Length: " << length << std::endl;
   data.normals = new Vector3[length];
 	
	for (i=0; i < length; i++) {
			mFile.read(buffer, 4);
			memcpy(&data.normals[i].x, buffer, 4);
 
			mFile.read(buffer, 4);
			memcpy(&data.normals[i].y, buffer, 4);
 
			mFile.read(buffer, 4);
			memcpy(&data.normals[i].z, buffer, 4);
	}
 
	// Get Texture Coordinates
 
	/*mFile.read(buffer, 4);
   memcpy(&length, buffer, 4);
   std::cout << "Texture UV Length: " << length << std::endl;
 
	data.texcoords = new Vector2[length];
 
	for (i=0; i < length; i++) {
		mFile.read(buffer, 4);
		memcpy(&data.texcoords[i].x, buffer, 4);
 
		mFile.read(buffer, 4);
		memcpy(&data.texcoords[i].y, buffer, 4);
	}*/
	
        
   unsigned int temp1;
   float temp2;
   int len = 8;
   if (version >= 15)
   	len = 5;
   	
	for (unsigned int i = 0; i < len; ++i) {
		mFile.read(buffer, 4);
	   memcpy(&temp1, buffer, 4);
	   std::cout << "   --Texture Coords " << i << ":" << temp1 << std::endl;
	   for (unsigned int j= 0; j < temp1*2; ++j) {
  			mFile.read(buffer, 4);
  			memcpy(&temp2, buffer, 4);
	   }
	}
 
	// Get Vertex indices
 
	mFile.read(buffer, 4);
	memcpy(&length, buffer, 4);
	std::cout << "vertex indices Length: " << length << std::endl; // <-- no idea wtf this is.. 
	data.unknown01 = new Vector2i[length];
	

	for (unsigned int i=0; i < length; i++) {
		mFile.read(buffer, 2);
	}

 
	mFile.read(buffer, 4);
	memcpy(&length, buffer, 4);
	
	std::cout << "submesh Length: " << length << std::endl;
	for (int i =0; i < length; i++) {
		mFile.read(buffer, 4);
		mFile.read(buffer, 4);
		mFile.read(buffer, 4);
		mFile.read(buffer, 4);
		mFile.read(buffer, 4);
	}

	// Get Unknown data 02
 
	mFile.read(buffer, 4);
	memcpy(&length, buffer, 4);
	std::cout << "face normals? Length: " << length << std::endl;
	
	data.unknown02 = new Vector2i[length*3];
 
	for (unsigned int i=0; i < length*3; i++) {
		mFile.read(buffer, 4);
		memcpy(&data.unknown02[i].x, buffer, 4);
	}

	// Get unknown data 03
	mFile.read(buffer, 4);
	memcpy(&length, buffer, 4);
	std::cout << "Edge count length: " << length << std::endl;
 
	data.unknown03 = new Vector2[length*3];
 
	for (unsigned int i=0; i < length*3; i++) {
		mFile.read(buffer, 4);

	}	
 
	// Get unknown data 04
 
	mFile.read(buffer, 4); 
	memcpy(&length, buffer, 4);
	std::cout << "tangentBasisCount length: " << length << "  <-- always zero" << std::endl;
	 
 	for (unsigned int i=0; i < length; i++) { // right length on 1, wrong number of bytes or maybe a for loop in here somewhere
 		for (unsigned int j=0; j < length; j++) {
 			// var[i,j] = foo
			mFile.read(buffer, 4);
		}
	}
	
//	if(version != 15){
//		mFile.read(buffer, 4);
//		mFile.read(buffer, 4);
//		mFile.read(buffer, 4);
//	}
 
	// Get unknown data 05 - maybe edges or faces

	mFile.read(buffer, 4);
	memcpy(&length, buffer, 4);
	std::cout << "Bone Names length: " << length << std::endl;
 
	for (unsigned int i=0; i < length; i++) {
		mFile.read(buffer, 2); // read two chars?
	}	
 
	// Get unknown data 06
	mFile.read(buffer, 4);
	memcpy(&length, buffer, 4);
	std::cout << "BoneIndex length: " << length << std::endl;
	for (unsigned int i=0; i < length; i++) {
		mFile.read(buffer, 1); // read two chars?
	}


	// Get unknown data 06
	mFile.read(buffer, 4);
	memcpy(&length, buffer, 4);
	std::cout << "Boneweight length: " << length << std::endl;
	for (unsigned int i=0; i < length; i++) {
		mFile.read(buffer, 1); // read two chars?
	}
	
	// Get unknown data 06
	mFile.read(buffer, 4);
	memcpy(&length, buffer, 4);
	std::cout << "autodrop vertex length: " << length << std::endl;
	for (unsigned int i=0; i < length; i++) {
		mFile.read(buffer, 2); // read two chars?
	}
	
	

/*	unsigned int length2;
	for (unsigned int i=0; i < length; i++) { // right length on 1, wrong number of bytes or maybe a for loop in here somewhere
		mFile.read(buffer, 4);
		memcpy(&length2, buffer, 4);
		std::cout << "unknown06_" << i <<" length: " << length2 << std::endl;
		for (unsigned int x=0; x < length2; x++) {
			mFile.read(buffer, 4);
			mFile.read(buffer, 4);
		}
	}*/
 
	unsigned int length2; 
	mFile.read(buffer, 2);
	memcpy(&length, buffer, 2);
	std::cout << "unknown14 length: " << length << std::endl;
	for (unsigned int i=0; i < length; i++) {// right length on 1, wrong number of bytes or maybe a for loop in here somewhere
		mFile.read(buffer, 1);
	}

	mFile.read(buffer, 4);
	memcpy(&length, buffer, 4);
	std::cout << "Shader length: " << length << std::endl;
	char name[128] = {0};
	for (unsigned int i=0; i < length; i++) { 
		mFile.read(buffer, 2);
		memcpy(&length2, buffer, 2);
		mFile.read(name, length2);
		std::cout << "   --- Shader Pallet" << i <<" name: " << name << std::endl;
	} 

	float tempfloat;
	for (short i = 1; i < 4; i++) {
		mFile.read(buffer, 4);
		memcpy(&tempfloat, buffer, 4);
		std::cout << " - Centeroid[" << i << "]: " << tempfloat << std::endl;
	}
	
	mFile.read(buffer, 4);
	memcpy(&tempfloat, buffer, 4);
	std::cout << " - Radius: " << tempfloat << std::endl;
	
	for (short i = 1; i < 4; i++) {
		mFile.read(buffer, 4);
		memcpy(&tempfloat, buffer, 4);
		std::cout << " - aabbMin[" << i << "]: " << tempfloat << std::endl;
	}	
	
	for (short i = 1; i < 4; i++) {
		mFile.read(buffer, 4);
		memcpy(&tempfloat, buffer, 4);
		std::cout << " - aabbMax[" << i << "]: " << tempfloat << std::endl;
	}
	
	if (version >= 12) {
		unsigned int degenerateVertexCount;
		unsigned int degenerateEdgeCount;
		
		mFile.read(buffer, 4);
		memcpy(&degenerateVertexCount, buffer, 4);
		std::cout << "Degenerate Vertex Count: " << degenerateVertexCount << std::endl;
		
		mFile.read(buffer, 4);
		memcpy(&degenerateEdgeCount, buffer, 4);
		std::cout << "Degenerate Edge Count: " << degenerateEdgeCount << std::endl;
	}
	
	if (version >= 13) {
		mFile.read(buffer, 4);
		memcpy(&length, buffer, 4);
		std::cout << "noPolygon Shadow Triangle Index Count: " << length << std::endl;

		unsigned short noPolygonShadowTriangleIndices[length];
      for (unsigned int i = 0; i < length; i++) { 
			mFile.read(buffer, 4);
			memcpy(&noPolygonShadowTriangleIndices, buffer, 4);
		}
	}
}
